package com.buba.other.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.other.service.impl.MedicalRecordsServiceImpl;
import com.buba.pojo.other.MedicalRecords;
import com.buba.pojoVo.MedicalRecordsVo;
import com.buba.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.buba.other.data.dto.MedicalRecordsDto;
import com.buba.other.data.dto.MedicalRecordsDetailsDto;
import com.buba.other.data.vo.GetMedicalRecordsMethodVo;
import com.buba.other.service.MedicalRecordsService;
import com.buba.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import com.buba.other.service.MedicalRecordsService;
import com.buba.pojo.other.MedicalRecords;
import com.buba.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 病历表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Slf4j
@RestController
@RequestMapping("/other/medical-records")
public class MedicalRecordsController {
    @Autowired
    private MedicalRecordsService medicalRecordsService;


    //添加病历
    @PostMapping("/addmedicalRecords")
    public R addmedicalRecords(@RequestBody MedicalRecords medicalRecords){
         boolean save = medicalRecordsService.save(medicalRecords);
        if(save==true){
            return R.ok().data("ok","添加成功");
        }else {
            return R.error().data("error","添加失败");
        }
    }

    @Autowired
    private MedicalRecordsService mrService;


    //@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    /**
     * todo 获取病例列表 根据 病人id和就诊时间段
     * @param vo
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/20 19:20
     */
    @RequestMapping(value = "/getMedicalRecords",method = RequestMethod.POST)
    public R getMedicalRecords(@RequestBody  GetMedicalRecordsMethodVo vo){
        log.info("++++++++++++++++++++++++++++++ MedicalRecordsController ++++++++++++++++++++++++++++++ :{},{}",vo);
        //List<MedicalRecordsDto> list = mrService.getMedicalRecords(dateRanges[0],dateRanges[1],patientId);
        List<MedicalRecordsDto> list = mrService.getMedicalRecords(vo.getStartDate(),vo.getEndDate(),vo.getPatientId());
        if(list!=null){
            return R.ok().data("list",list);
        }
        else {
            return R.error();
        }

    }
    /**
     * todo 获取病例id 获取详细病情
     * @param medicalRecordsId
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/20 19:27
     */
    @RequestMapping(value = "/getMedicalRecordsDetails",method = RequestMethod.POST)
    public R getMedicalRecordsDetails(@RequestParam("id") Integer medicalRecordsId){
        MedicalRecordsDetailsDto medicalRecordsDetails = mrService.getMedicalRecordsDetails(medicalRecordsId);
        if(medicalRecordsDetails!=null){
            return R.ok().data("medicalRecordsDetails",medicalRecordsDetails);
        }else{
            return R.error();
        }
    }




    /*
     * @Author: 张庆威
     * @Description: (根据就诊人id 查询所有历史病历)
     * @Date: 19:52 2022/6/22
     * @Param: [patientId]
     * @return: com.buba.utils.R
     **/
    @GetMapping("/getBinglis/{patientId}")
    public R getBinglis(@PathVariable("patientId") Integer patientId){
        log.info("patientId:::::::"+patientId);
//        QueryWrapper<MedicalRecords> wrapper = new QueryWrapper<>();
//        wrapper.eq("patient_id",patientId);
//        List<MedicalRecords> list = medicalRecordsService.list(wrapper);
        List<MedicalRecordsVo> list = medicalRecordsService.getBinglis(patientId);

        if(list != null){
            return R.ok().data("list",list);
        }else {
            return R.error();
        }

    }


}

